﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using System.IO;

public partial class controls_admin_ModuleEdit : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadSetting();
                loadTypeModule();
                base.loadCategories(ddlCategories, 0);
                loadCategories();
                if (Request["c_id"] != null)
                    ddlCategories.SelectedValue = Request["c_id"].ToString();
                loadModule();
            }
            catch { }
            finally { base.CloseConnection(); }
        }
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(2, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_CATEGORY":
                    NOTE_CATEGORY.InnerText = value;
                    break;
                case "NOTE_DISPLAY_DETAILS":
                    NOTE_DISPLAY_DETAILS.InnerText = value;
                    break;
                case "NOTE_DISPLAY_HOME":
                    NOTE_DISPLAY_HOME.InnerText = value;
                    break;
                case "NOTE_LOCATION":
                    NOTE_LOCATION.InnerText = value;
                    break;
                case "NOTE_MODULE_CONTENT":
                    NOTE_MODULE_CONTENT.InnerText = value;
                    break;
                case "NOTE_PAGE_SIZE":
                    NOTE_PAGE_SIZE.InnerText = value;
                    break;
                //case "Note_Sort_Display":
                //    stgSortDisplay.InnerText = value;
                //    break;
                case "NOTE_TITLE":
                    NOTE_TITLE.InnerText = value;
                    break;
                case "NOTE_TYPE":
                    NOTE_TYPE.InnerText = value;
                    break;
            }
        }
    }
    private void loadCategories()
    {
        try
        {
            DataTable objDT = new DataTable();
            objDT = BO_Categories.SelectParent(0, 1);
            lbxCategories.Items.Clear();
            lbxCategories.Items.Add(new ListItem("Đăng ký", "-3"));
            lbxCategories.Items.Add(new ListItem("Quên mật khẩu", "-4"));
            lbxCategories.Items.Add(new ListItem("Shows Chi tiết", "-5"));
            lbxCategories.Items.Add(new ListItem("Tin chi tiết", "-6"));
            lbxCategories.Items.Add(new ListItem("Liên hệ", "-7"));
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow dr in objDT.Rows)
                {
                    lbxCategories.Items.Add(new ListItem(dr["FULL_CATEGORY_NAME"].ToString(), dr["CATEGORY_ID"].ToString()));
                    loadChildCategories(Convert.ToInt32(dr["CATEGORY_ID"].ToString()), "");
                }
            }
        }
        catch { }
    }
    private void loadChildCategories(int c_c_id, string ident)
    {
        try
        {
            DataTable c_categoryDT = BO_Categories.SelectParent(c_c_id, 1);
            if (c_categoryDT != null && c_categoryDT.Rows.Count > 0)
            {
                ident += "--- ";
                foreach (DataRow drChild in c_categoryDT.Rows)
                {
                    lbxCategories.Items.Add(new ListItem(ident + drChild["FULL_CATEGORY_NAME"].ToString(), drChild["CATEGORY_ID"].ToString()));
                    loadChildCategories(Convert.ToInt32(drChild["CATEGORY_ID"].ToString()), ident);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void loadTypeModule()
    {
        try
        {
            DataTable dtTypeModule = BO_Module_Type.SelectAll(1);
            ddlType.DataSource = dtTypeModule;
            ddlType.DataTextField = "TYPE_DESCRIPTION";
            ddlType.DataValueField = "TYPE_ID";
            ddlType.DataBind();
        }
        catch { }
    }
    protected void btnModuleAction_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable objDT = BO_Module.Search(Convert.ToInt32(Request["m_id"]));
            if (objDT == null || objDT.Rows.Count <= 0)
            {
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Bản ghi không tồn tại hoặc đã bị xóa");
                return;
            }
            string fileSale = string.Empty;
            string fileLeasing = string.Empty;
            //string s_s_d = "1";// txtSDHome.Text.Trim();
            int type_id = Convert.ToInt32(ddlType.SelectedValue);
            string title = txtTitle.Text.Trim();
            int moduleType = Convert.ToInt32(ddlType.SelectedValue);
            string c_id = String.Empty;
            string position = ddlLocation.SelectedValue;
            string sort_display = objDT.Rows[0]["SORT_DISPLAY"].ToString(); //"-1:" + position + ":" + s_s_d + ";-2:" + position + ":" + s_s_d + ";";
            foreach (ListItem item in lbxCategories.Items)
            {
                if (item.Selected)
                {
                    c_id = c_id + item.Value + ";";
                    //sort_display = sort_display + item.Value + ":" + position + ":" + s_s_d + ";";
                }
            }
            int module_content = Convert.ToInt32(ddlCategories.SelectedValue);
            string botBG = string.Empty;
            string fontFamily = "";// ddlFont.Text;
            int fontSize = 1;// Convert.ToInt32(txtFontSize.Text);
            string fontStyle = "";// ddlFontWeight.Text;
            string fontColor = "";//txtColor.Text.Trim();
            string smsSystax = "";// txtSMS.Text.Trim();
            string readMore = string.Empty;
            int page_size = Convert.ToInt32(txtPageSize.Text);
            bool isHome = cboDisplay.Checked;
            bool isDetails = cboDisDetails.Checked;
            DataTable admin = (DataTable)Session["Admin"];
            string strresult = BO_Module.Update(Convert.ToInt32(Request["m_id"]), type_id, title, c_id, module_content, position, fileSale, fileLeasing, botBG, fontFamily, fontColor, fontSize, fontStyle, smsSystax, page_size, readMore, isDetails, isHome, sort_display, Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), true);
            if (strresult != "0")
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Module đã được cập nhật!");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Phát sinh lỗi trong quá trình xử lý!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    private void loadModule()
    {
        try
        {
            if (Request["m_id"] == null)
            {
                divMessage.InnerHtml = O2S_Message.Warning("Cảnh Báo", "Trang này không tồn tại hoặc đã bị xóa !");
                return;
            }
            DataTable objDT = BO_Module.Search(Convert.ToInt32(Request["m_id"]));
            if (objDT != null && objDT.Rows.Count > 0)
            {
                txtTitle.Text = objDT.Rows[0]["FULL_NAME"].ToString();
                ddlType.SelectedValue = objDT.Rows[0]["TYPE_ID"].ToString();
                ddlCategories.SelectedValue = objDT.Rows[0]["MODULE_CONTENT"].ToString();
                ddlLocation.SelectedValue = objDT.Rows[0]["LOCATION"].ToString();
                //txtSMS.Text = objDT.Rows[0]["SMS_SYSTAX"].ToString();
                if (Convert.ToBoolean(objDT.Rows[0]["IS_DISPLAY_ON_DETAIL"]))
                    cboDisDetails.Checked = true;
                else
                    cboDisDetails.Checked = true;
                if (Convert.ToBoolean(objDT.Rows[0]["IS_DISPLAY_ON_HOME_PAGE"]))
                    cboDisplay.Checked = true;
                else
                    cboDisplay.Checked = true;
                //rdoWapYes.Checked = Convert.ToBoolean(objDT.Rows[0]["IS_WAPSITE"]);
                //txtSDHome.Text = "1";
                //txtColor.Text = objDT.Rows[0]["FONT_COLOR"].ToString();
                //ddlFontWeight.Text = objDT.Rows[0]["FONT_WEIGHT"].ToString();
                //ddlFont.Text = objDT.Rows[0]["FONT_FAMILY"].ToString();
                //txtFontSize.Text = objDT.Rows[0]["FONT_SIZE"].ToString();
                txtPageSize.Text = objDT.Rows[0]["PAGE_SIZE"].ToString();
                string category = objDT.Rows[0]["CATEGORY_ID"].ToString();
                string[] arrCategory = category.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (ListItem item in lbxCategories.Items)
                {
                    foreach (string perCategory in arrCategory)
                    {
                        if (item.Value.Equals(perCategory))
                        {
                            item.Selected = true;
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { }
    }
}
